/*
 * Document load script, auto included with mixer template
 */

var player = new Array();

$(document).ready(function() {
	debug.print("$(document).ready");
	
	// save window title
	config.title = document.title;
		
	// lazy load js
	scripts = ["share.js", "download.js", "fav.js", "actions.js", "tabs.js", "launcher.js", "ui.js",
		"timer.js", "playlist.js", "controller.js", "search_videos.js", "search_videos_list.js",
		"np.js", "autoplay.js", "autodj.js", "enhance.js"];
	// don't rely in prefs right now due to chrome bug (see style/vdjo.php)
	// TODO: check if these scripts have been loaded to begin clearing ui
	if (config.compress == true) {
		var qs = '';
		for (var i in scripts) {
			qs = qs + 'templates/mixer/js/' + scripts[i] + '&';
		}
		var element = document.createElement("script");
		element.src = "/compress.php?" + qs;
		document.body.appendChild(element);
	} else {
		for (var i in scripts) {
	 		var element = document.createElement("script");
	 		element.src = "/templates/mixer/js/" + scripts[i];
	 		document.body.appendChild(element);
		}
	}
	
	// load player apis
	var params = { allowScriptAccess: "always", wmode: "transparent" };
	swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=vid0-player",
		"vid0-object", "425", "356", "8", null, null, params, { id: "vid0-player" }, check.embedCheck);
	swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=vid1-player",
		"vid1-object", "425", "356", "8", null, null, params, { id: "vid1-player" });
	
	/**
	 * player
	 * @type {playerInterface}
	 */
	player = [new playerInterface(0), new playerInterface(1)];
	
	// check for compatibility issues
	check.systemCheck();
	
	var loadingCheck = setTimeout(function() {
		flash.modal("There was an error loading vdjo.com", "You can wait, but if something is not right, it can take a looooong time. Or you can <a href='javascript:document.location.href=document.location.href;'>reload this page</a> (<u>F5</u> works, too) and hope for the best!");
	}, 10000);
	$(document).bind("vdjoReady", function() { clearTimeout(loadingCheck); });
	
});

function other(key) {
	if (key == 0) {
		return 1;
	} else {
		return 0;
	}
}

function onYouTubePlayerReady(playerid) {
	debug.print("onYouTubePlayerReady [playerid: " + playerid + "]");
	playerCounter.increase()
	if (playerCounter.count() == 2) {
		player[0].setApi(document.getElementById("vid0-player"));
		player[1].setApi(document.getElementById("vid1-player"));
		player[0].init();
		player[1].init();
		config.init();
		
		debug.print("All players loaded", true);
		
		// unset loading state
		$("#contents").removeClass("loading");
		$("#contents-loading p").text("ready...").parent().fadeOut();
		
		// launcher
		if (config.launcher == true) {
			setTimeout(launcher.show, 250);
		}
		
		// init controller
		bind.init();
		
		// init ui
		ui.init();
		
		// init timer
		timer.start();
		
		// init tabs brfore vdjoReady event, so we can override from hooked functions there
		tabs.show('search');
		
		// trigger vdjo ready event
		$(document).trigger("vdjoReady");
	}	
}

var playerCounter = {
	counter: 0,
	increase: function(){
		debug.print("playerCounter.increase");
		this.counter++;
	},
	count: function(){
		debug.print("playerCounter.count");
		return this.counter;
	}
}